package logiexport.paneles;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import logiexport.pojos.User;
import logiexport.bd.*;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;



public class PanelRegister extends Panel {
	
	User u = new User();
	App app = new App();

	public PanelRegister(String id) {
		super(id);
		// TODO Auto-generated constructor stub
		
		 Form<User> registerform =  new Form<User>("registerform", new CompoundPropertyModel<User>(u)) {
			 
			 protected void onSubmit() {
					// Al hacer submit verifico que existe un usuario con ese password 
					
					System.out.println("------Nombre"+u.getName()+" Empresa"+u.getEmpresa());
					
					app.adduser(u);
					
					String codigo ="Jorge";
					
					
					DefaultHttpClient httpClient = new DefaultHttpClient();

					HttpPost request = new HttpPost(
							"http://localhost/pdf/confirmacion.php");

					List<NameValuePair> postParams = new ArrayList<NameValuePair>();

					postParams.add(new BasicNameValuePair("codigo", codigo));
					

					UrlEncodedFormEntity entity;
					try {
						entity = new UrlEncodedFormEntity(
								postParams);
						
						entity.setContentEncoding(HTTP.UTF_8);
						entity.setContentType("application/json");
						request.setEntity(entity);

						request.addHeader("content-type",
								"application/x-www-form-urlencoded");

						HttpResponse response = httpClient.execute(request);
			

						System.out.println("Estado: "
								+ response.getStatusLine().getStatusCode());

						BufferedReader rd = new BufferedReader(
								new InputStreamReader(response.getEntity()
										.getContent()));
						String line = "";
						while ((line = rd.readLine()) != null) {
							System.out.println(line);
						}

						
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalStateException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				
				}	
			 
		 };
			

				add(registerform);
				
				TextField<User> nombre =  new TextField<User>("name");
				TextField<User> email =  new TextField<User>("email");
				TextField<User> tele =  new TextField<User>("tele");
				TextField<User> poblacion =  new TextField<User>("poblacion");
				TextField<User> provincia =  new TextField<User>("provincia");
				TextField<User> direccion =  new TextField<User>("direccion");
				TextField<User> cp =  new TextField<User>("cp");
				TextField<User> empresa =  new TextField<User>("empresa");
				PasswordTextField pass =  new PasswordTextField("pass");
				
				
				registerform.add(nombre);
				registerform.add(email);
				registerform.add(tele);
				registerform.add(poblacion);
				registerform.add(provincia);
				registerform.add(direccion);
				registerform.add(cp);
				registerform.add(empresa);
				registerform.add(pass);
		
		
		
	}
	
	

}
